//
//  MTZTabBar.swift
//  weibomtz
//
//  Created by 毛铁柱 on 16/10/14.
//  Copyright © 2016年 mtz. All rights reserved.
//

import UIKit

class MTZTabBar: UITabBar {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    //撰写按钮点击要执行的闭包
    var composeButtonClickClourse:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
        

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置撰写按钮的位置
    override func layoutSubviews() {
        //重写之前一定要super一下
        super.layoutSubviews()
        // 以后开发中，子控制的center尽量不要用父控制的center去计算因为参照物不一样
        composeButton.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
        
//        print("===========")
        
        // 计算出子按钮的宽度
        let childW:CGFloat = self.frame.size.width * 0.2
        // 记录遍历到第几个按钮
        var index:CGFloat = 0
        
        for childView in subviews{
//             print(childView)
            
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                childView.frame.size.width = childW
                childView.frame.origin.x = index * childW
                index++
                if index==2 {
                    index=3
                }
            }
        
        }
    }
    
    @objc private func composeButtonClick(){
    
        print("撰写按钮点击")
        //执行闭包
        composeButtonClickClourse?()
    
    }
    
    //撰写按钮
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        
       button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"),forState: UIControlState.Normal)
     button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
     return button
    
    
    }()

}
